<?php
namespace app\index\controller;

use \app\back\model\Account;
use \app\back\model\AccountTickets;
use \app\back\model\AccountRecord;
use \app\index\model\Account as MemberModel;

class Member extends \app\common\controller\BaseController
{
    public function index()
    {

    	$openid = session('userinfo.openid');

    	$user_model = new Account();
    	$userinfo = $user_model->getInfoByOpenid($openid);

        return $this->fetch('', ['userinfo'=>$userinfo]);
    }

    /*
     * 会员券
     */
    function tickets() {
        $mobile = session('userinfo.mobile');

        $ticket_model = new AccountTickets();
        $tickets = $ticket_model->getActiveTicket($mobile);

        return $this->fetch('', ['pagetitle'=>'会员券管理', 'tickets'=>$tickets]);
    }

    /*
     * 查询记录
     */
    function record($type='notype') {
        $list = [];
        $mobile = session('userinfo.mobile');

        switch ($type) {
            case 'recharge':
                $types = [2,3];
                break;
            case 'cost':
                $types = [4];
                break;
            case 'voucher':
                $types = [7];
                break;
            default:
                $types = [-1];
                break;
        }

        $record_model = new AccountRecord();
        $list = $record_model->where('mobile',$mobile)->where('recharge_type', 'in', $types)->order('create_time', 'desc')->select();

        return $this->fetch('', ['list'=>$list]);
    }

    /*
     * 店铺活动
     */
    function active() {
        $list = [];
        return $this->fetch('', ['list'=>$list]);
    }

    /*
	 * 会员设置
     */
    function setting() {
    	$success = $error = null;
    	$openid = session('userinfo.openid');

    	$member_model = MemberModel::where('wx_openid', $openid)->field('nickname,username,gender,birthday')->find();

    	if(request()->isPost()) {
    		$_POST['nickname'] = $member_model->nickname;
    		$member_model->allowField(['username', 'gender', 'birthday'])->data($_POST);

    		$validate = new \think\Validate($this->settingRule(), $this->msgRule());

    		if (!$validate->check($_POST)) {
			    $error = $validate->getError();
				return ['state'=>false, 'message'=>$error, 'token'=>$this->request->token('__token__', 'sha1')];
			}

    		if($member_model->save()) 
    			return ['state'=>true, 'message'=>'个人信息修改成功', 'token'=>$this->request->token('__token__', 'sha1')];
    		else
    			return ['state'=>false, 'message'=>'修改失败，再来一次吧', 'token'=>$this->request->token('__token__', 'sha1')];

    	}
        
        $token = $this->request->token('__token__', 'sha1');
    	return $this->fetch('', ['member_model'=>$member_model, 'error'=>$error, 'success'=>$success, 'pagetitle'=>'个人信息', 'today'=>date('Y-m-d'), 'token'=>$token]);
    }

    private function settingRule() {
    	return [
            'username'  => 'require|max:15', 
            'gender'    => ['number','between'=>'0,2'],
            'birthday'  => 'require|date'
        ];
    }

    private function msgRule() {
    	return [
            'username.require'  => '请填写姓名', 
            'username.token'  => '令牌无效，点出刷新令牌', 
            'username.max'  => '姓名最长5个汉字', 
            'username.number'   => '性别错了，重新选择', 
            'username.between'  => '性别真见鬼了，再试一次', 
            'birthday.date'  => '不合法的日期',
            'birthday.require'  => '请选择出生日期'
        ];
    }
}
